In this tutorial I will show you how to generate OTP code in c# mvc. Nowadays most applications are using two factor verification to validate the user’s login information using mobile number for online banking or made purchase online goods.
In order to implement the otp verification in c# mvc. Add an empty controller by right click on the controllers folder and name as your wish I named as OTP. Copy and paste the following.
[HttpPost]
public ActionResult GetNumericOTP()
{
string numbers = "0123456789";
Random random = new Random();
string otp = string.Empty;
for (int i = 0; i < 5; i++)
{
int tempval = random.Next(0, numbers.Length);
otp += tempval;
}
return Json(otp, JsonRequestBehavior.AllowGet);
}
Right click on the index and add a view. Copy and paste the following. Here I have implemented using jQuery ajax method make a call to a controller function GetNumericOTP function return as json string result.
@{
ViewBag.Title = "Generate Numeric OTP";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
@using (Html.BeginForm())
{
<table>
<tr>
<td>Click on button to Generate Numeric OTP :</td>
<td><input id="btnGenerateOTP" type="submit" value="Generate OTP" /></td>
</tr>
<tr>
<td></td>
<td><p style="color:mediumvioletred" id="result"></p></td>
</tr>
</table>
}
<script type="text/javascript">
$(function () {
$("#btnGenerateOTP").click(function () {
$.ajax({
type: 'POST',
url: '/OTP/GetNumericOTP',
data: {},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
$("#result").html(response);
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
return false;
})
});
</script>
Below video explain about how to implement otp in mvc.
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article